<?php

Raise::load('core.io.file.RaisePath');

/**
 * TestCoreRaisePath class
 * Testing unit of 'phpraise/core/RaisePath.php'
 *
 * @author Sam-Mauris Yong / hellclanner at live dot com
 * @license http://www.opensource.org/licenses/bsd-license New BSD License
 * @package Raise.UnitTest.Test
 */
class TestCoreRaisePath extends RaiseTest {

    public static function testClass(){
        self::assertClassExists('RaisePath');
        self::assertClassExtends('RaisePath', 'RaiseStaticClass');

        self::assertConstantDefined('RaisePath::DIRECTORY');
        self::assertConstantDefined('RaisePath::BASENAME');
        self::assertConstantDefined('RaisePath::EXTENSION');
        self::assertConstantDefined('RaisePath::FILENAME');
    }

    public static function testPathCombine(){
        if(RaisePath::directorySeparator() == '\\'){
            self::addMessage('Path tested are based on Windows platform. Please also test the paths on Linux.');
            self::assertStrictEqual(RaisePath::combine('C:\\windows\\system32\\', '\\'), 'C:\\windows\\system32');
            self::assertStrictEqual(RaisePath::combine('C:\\windows\\system32', '../..'), 'C:\\');
            self::assertStrictEqual(RaisePath::combine('C:\\windows\\system32\\', '../..'), 'C:\\');
            self::assertStrictEqual(RaisePath::combine('C:\\windows\\system32', '..'), 'C:\\windows');
            self::assertStrictEqual(RaisePath::combine('C:\\windows/system32', '..'), 'C:\\windows');
            self::assertStrictEqual(RaisePath::combine('C:\\windows/system32', '..//'), 'C:\\windows');
            self::assertNotEqual(RaisePath::combine('C:\\windows\\system32\\', '\\'), 'C:\\windows\\system32\\');
            self::assertNotEqual(RaisePath::combine('C:\\windows\\system32\\', '..\\..'), 'C:\\windows\\system32\\');
        }else{
            self::addMessage('Path tested are based on *nix platform. Please also test the paths on Windows.');
            self::assertStrictEqual(RaisePath::combine('/var/www/admin/panel', '../..'), '/var/www');
            self::assertStrictEqual(RaisePath::combine('/var/www/admin/panel/', '../..'), 'C:\\');
            self::assertStrictEqual(RaisePath::combine('/var/www/admin/panel', '..'), '/var/www/admin');
            self::assertStrictEqual(RaisePath::combine('/var/www/admin\\panel', '..'), '/var/www/admin');
            self::assertStrictEqual(RaisePath::combine('/var/www/admin\\panel', '..//'), '/var/www/admin');
            self::assertNotEqual(RaisePath::combine('/var/www/admin/panel', '\\'), '/var/www/admin/panel/');
            self::assertNotEqual(RaisePath::combine('/var/www/admin/panel', '..\\..'), '/var/www/admin/panel/');
        }
        self::assertStrictEqual(RaisePath::combine('', '..\\..'), RaisePath::combine(getcwd(), '..\\..'));
    }

    public static function testConstants(){
        self::assertStrictEqual(RaisePath::directorySeparator(), DIRECTORY_SEPARATOR);
        self::assertType(RaisePath::directorySeparator(), 'string');
        self::assertStrictEqual(RaisePath::pathSeparator(), PATH_SEPARATOR);
        self::assertType(RaisePath::pathSeparator(), 'string');
        self::assertStrictEqual(RaisePath::tempPath(), sys_get_temp_dir());
        self::assertType(RaisePath::tempPath(), 'string');

        self::assertStrictEqual(RaisePath::BASENAME, 'basename');
        self::assertStrictEqual(RaisePath::FILENAME, 'filename');
        self::assertStrictEqual(RaisePath::DIRECTORY, 'dirname');
        self::assertStrictEqual(RaisePath::EXTENSION, 'extension');
    }

    public static function testCreateDeletePath(){
        $fp = RaisePath::combine(RaisePath::tempPath(), 'testPath');
        $p = RaisePath::create($fp);
        self::assertTrue($p);
        self::assertTrue(RaiseFileSystem::pathExist($fp));
        $p = RaisePath::delete($fp);
        self::assertTrue($p);
        self::assertFalse(RaiseFileSystem::pathExist($fp));

        $fp = RaisePath::combine(RaisePath::tempPath(), 'testPath/test2');
        $p = RaisePath::create($fp);
        self::assertTrue($p);
        self::assertTrue(RaiseFileSystem::pathExist($fp));
        $p = @RaisePath::delete(RaisePath::combine($fp, '..'));
        self::assertFalse($p);
        self::assertTrue(RaiseFileSystem::pathExist($fp));
        RaisePath::delete($fp);
        RaisePath::delete(RaisePath::combine($fp, '..'));
    }

    public static function testEmptyPath(){
        $fp = RaisePath::combine(RaisePath::tempPath(), 'testPath/test2/');
        $p = RaisePath::create($fp);
        self::assertTrue($p);
        self::assertTrue(RaiseFileSystem::pathExist($fp));

        $file1 = new RaiseFile('mocks/MockConfigFile.ini');
        $file1 = $file1->copy($fp);
        $file2 = new RaiseFile('mocks/MockRaiseUIControl.php');
        $file2 = $file2->copy($fp);

        self::assertTrue($file1->exists());
        self::assertTrue($file2->exists());

        $fp = RaisePath::combine($fp, '..');
        RaisePath::clear($fp);

        self::assertFalse($file1->exists());
        self::assertFalse($file2->exists());
        
        self::assertFalse(RaiseFileSystem::pathExist(RaisePath::combine($fp, 'test2')));
        $p = RaisePath::delete($fp);
        self::assertTrue($p);
        self::assertFalse(RaiseFileSystem::pathExist($fp));
    }

    public static function testPathInfo(){
        $path = 'C:\\windows\\explorer.exe';
        self::assertStrictEqual(RaisePath::pathInfo($path, RaisePath::BASENAME), 'explorer.exe');
        self::assertStrictEqual(RaisePath::pathInfo($path, RaisePath::FILENAME), 'explorer');
        self::assertStrictEqual(RaisePath::pathInfo($path, RaisePath::EXTENSION), 'exe');
        self::assertStrictEqual(RaisePath::pathInfo($path, RaisePath::DIRECTORY), 'C:\\windows');
        self::assertStrictEqual(RaisePath::pathInfo($path, RaisePath::DIRECTORY), RaisePath::path($path));
        self::assertStrictEqual(RaisePath::pathInfo($path, RaisePath::BASENAME), RaisePath::baseName($path));

        $path = '/user/www/test/home.html';
        self::assertStrictEqual(RaisePath::pathInfo($path, RaisePath::BASENAME), 'home.html');
        self::assertStrictEqual(RaisePath::pathInfo($path, RaisePath::FILENAME), 'home');
        self::assertStrictEqual(RaisePath::pathInfo($path, RaisePath::EXTENSION), 'html');
        self::assertStrictEqual(RaisePath::pathInfo($path, RaisePath::DIRECTORY), '/user/www/test');
        self::assertStrictEqual(RaisePath::pathInfo($path, RaisePath::DIRECTORY), RaisePath::path($path));
        self::assertStrictEqual(RaisePath::pathInfo($path, RaisePath::BASENAME), RaisePath::baseName($path));

        $path = '..\\tool\\test.exe';
        self::assertStrictEqual(RaisePath::pathInfo($path, RaisePath::BASENAME), 'test.exe');
        self::assertStrictEqual(RaisePath::pathInfo($path, RaisePath::FILENAME), 'test');
        self::assertStrictEqual(RaisePath::pathInfo($path, RaisePath::EXTENSION), 'exe');
        self::assertStrictEqual(RaisePath::pathInfo($path, RaisePath::DIRECTORY), '..\\tool');
        self::assertStrictEqual(RaisePath::pathInfo($path, RaisePath::DIRECTORY), RaisePath::path($path));
        self::assertStrictEqual(RaisePath::pathInfo($path, RaisePath::BASENAME), RaisePath::baseName($path));

        $path = '../data/raw.bin';
        self::assertStrictEqual(RaisePath::pathInfo($path, RaisePath::BASENAME), 'raw.bin');
        self::assertStrictEqual(RaisePath::pathInfo($path, RaisePath::FILENAME), 'raw');
        self::assertStrictEqual(RaisePath::pathInfo($path, RaisePath::EXTENSION), 'bin');
        self::assertStrictEqual(RaisePath::pathInfo($path, RaisePath::DIRECTORY), '../data');
        self::assertStrictEqual(RaisePath::pathInfo($path, RaisePath::DIRECTORY), RaisePath::path($path));
        self::assertStrictEqual(RaisePath::pathInfo($path, RaisePath::BASENAME), RaisePath::baseName($path));
    }

    public static function testClassPath(){
        self::assertStrictEqual(RaisePath::classPathName('TestCoreRaisePath'), __FILE__);
        self::assertStrictEqual(RaisePath::classPathName('Raise'), RaisePath::combine(RaisePath::phpRaisePath(), 'Raise.php'));
    }

}

